package dcds2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import java.sql.*;
import java.lang.*;

/**
 *
 * @author Mesan
 */
public class AddProcedureWindow extends javax.swing.JFrame {

    /** Creates new form AddProcedureWindow */
    public AddProcedureWindow() {
        initComponents();
        setLocationRelativeTo(null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtProcedureName = new javax.swing.JTextField();
        txtProcedurePrice = new javax.swing.JTextField();
        btnConfirm = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("DCDS - Add Procedure");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        jLabel1.setFont(new java.awt.Font("HelveticaNeueLT Std Med", 1, 14));
        jLabel1.setText("Procedure Name:");

        jLabel2.setFont(new java.awt.Font("HelveticaNeueLT Std Med", 1, 14));
        jLabel2.setText("Price:");

        txtProcedurePrice.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtProcedurePriceKeyTyped(evt);
            }
        });

        btnConfirm.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonConfirm.png"))); // NOI18N
        btnConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmActionPerformed(evt);
            }
        });

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonBack.png"))); // NOI18N
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtProcedurePrice, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(txtProcedureName, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtProcedureName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtProcedurePrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        dispose();
        new MainWindow(2).setVisible(true);
}//GEN-LAST:event_btnBackActionPerformed

private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmActionPerformed
    JFrame prompt = new JFrame();
      
     int errCtr = 0;
     int selection = JOptionPane.showConfirmDialog(prompt, "Are all the fields correct?", "Confirmation", JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE);
     
     if(selection == JOptionPane.YES_OPTION){
     
         int procedureID = 1    ;
         
         String procedureName = "\"" + txtProcedureName.getText() + "\"";
         String procedurePrice = "\"" + txtProcedurePrice.getText() +"\"";
         
         //ERROR CHECKING//
         
         try{
             
             ConnectionFactory myFactory = ConnectionFactory.getFactory();
             Connection conn = myFactory.getConnection();
             
             boolean result = false;
            
             String checkName = txtProcedureName.getText();
             
             PreparedStatement pstmt = conn.prepareStatement("SELECT procedureName FROM dcds.procedures WHERE procedureName = '"+checkName+"'");
             ResultSet rs = pstmt.executeQuery();
             
             if(!rs.last()) {                        
                 // CHECKS IF THERE IS INPUT IN PROCEDURE NAME TEXTBOX 
                 if(txtProcedureName.getText().length() == 0){
                           errCtr++;
                           JOptionPane.showMessageDialog(prompt, "Please enter a procedure name!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                 }

                 //CHECK IF THERE IS AN INPUT IN PROCEDURE PRICE TEXTBOX
                 if(txtProcedurePrice.getText().length() == 0){
                           errCtr++;
                           JOptionPane.showMessageDialog(prompt, "Please enter a price!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                 }
                 else{
                     try{
                        double pp = Double.parseDouble(txtProcedurePrice.getText());
                     }
                     catch (NumberFormatException nfe){
                        errCtr++;
                     JOptionPane.showMessageDialog(prompt, "Please enter a valid price!","NOTICE!",JOptionPane.PLAIN_MESSAGE);  
                     }
                 }
             }
             else{
                 // CHECKS IF INPUT IS ALREADY EXISTING IN THE DATABASE
                 JOptionPane.showConfirmDialog(prompt, "Procedure already exists!", "NOTICE!", JOptionPane.PLAIN_MESSAGE);
                 errCtr++;
             }
         
         }catch(Exception ex){ ex.printStackTrace(); }
         
         //ERROR CHECKING//
         
         if (errCtr == 0){
         try{
             
             ConnectionFactory myFactory = ConnectionFactory.getFactory();
             Connection conn = myFactory.getConnection();
             boolean result = false;

             PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dcds.procedures");
             ResultSet rs = pstmt.executeQuery();
             while(rs.next()){
                 
                 procedureID = rs.getInt("procedureID")+1;
             }
             
             int res = pstmt.executeUpdate("INSERT into procedures(procedureID, procedureName, procedurePrice) values ("+procedureID+", "+procedureName+", "+procedurePrice+")");

             conn.close();
             result = true;
             
         }catch(Exception e){
             e.printStackTrace();
         }
        
          JOptionPane.showMessageDialog(prompt, "You have successfully added a new procedure!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
          dispose();
          new MainWindow(2).setVisible(true);
         }
         
         else if (errCtr > 0){
             errCtr = 0;
         }
     }
}//GEN-LAST:event_btnConfirmActionPerformed

private void txtProcedurePriceKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtProcedurePriceKeyTyped
    if (evt.getKeyChar() == java.awt.event.KeyEvent.VK_ENTER) {
        btnConfirm.doClick();
    }
}//GEN-LAST:event_txtProcedurePriceKeyTyped

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddProcedureWindow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnConfirm;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtProcedureName;
    private javax.swing.JTextField txtProcedurePrice;
    // End of variables declaration//GEN-END:variables
    private Connection connection;
    private ResultSet rs;
    private Statement stat;
}
